﻿using System;
using System.Collections.Generic;
using Assist;
using Autodesk.Revit.DB;
using YJKRoomInspection.Model;
using YJKRoomInspection.Properties;
using YJKRoomInspection.Utiltiy;
using YArchitech.Revit;
using YJKRevitKernel.ElementSearchUtility.LinkDocumentSuite;
using YJKGeometryFuncs.CurveUtility;
using YJKInspectModel.ModelValidation.Walls;
using YJKInspectModelFunc;

namespace YJKRoomInspection.FireAreOutLineAppend
{
	internal static class BaseReplacer
	{
		internal static bool CloseFireAreaWalls(ViewPlan useViewPlan)
		{
			Document document = useViewPlan.Document;
			StaticRescource.CmdData.Application.ActiveUIDocument.ActiveView = useViewPlan;
			foreach (Area area in YJKInspectModelFunc.Utility.GetAllAreaByView(useViewPlan))
			{
				List<Curve> list = new List<Curve>();
				foreach (IList<BoundarySegment> list2 in area.GetBoundarySegments(new SpatialElementBoundaryOptions()))
				{
					foreach (BoundarySegment segment in list2)
					{
						list.Add(segment.GetCurve());
					}
				}
				string name = area.Name;
				List<BaseLinkDocumentResultBean> allWallByView = YJKInspectModelFunc.Utility.GetAllWallByView(useViewPlan);
				List<Wall> list3 = new List<Wall>();
				int num = 0;
				foreach (BaseLinkDocumentResultBean baseLinkDocumentResultBean in allWallByView)
				{
					if (baseLinkDocumentResultBean.UseLinkeInstance == null)
					{
						Wall wall = baseLinkDocumentResultBean.UseElement as Wall;
						Curve curve = (wall.Location as LocationCurve).Curve;
						curve.GetEndPoint(0);
						curve.GetEndPoint(1);
						num++;
						int num2 = 0;
						foreach (Curve curve2 in list)
						{
							num2++;
							curve2.GetEndPoint(0);
							curve2.GetEndPoint(1);
							if (CurveUtility.IsCurveCrossWall(curve2, wall, baseLinkDocumentResultBean.UseLinkeInstance, baseLinkDocumentResultBean.GetTotalTransform()) && list3.IndexOf(wall) < 0)
							{
								list3.Add(wall);
							}
						}
					}
				}
				double val = 3000.0;
				if (list3.Count > 0)
				{
					WallsValidationManager wallsValidationManager = new WallsValidationManager(StaticRescource.CmdData);
					List<ElementId> list4 = new List<ElementId>();
					wallsValidationManager.WallClosedValidation(list3, AssistFunc.mmToFeet(val), ref list4);
				}
			}
			return true;
		}

		internal static bool ReplaceFamilyForFamilyInstance(List<AdapteDoorResultPakcer> inputValues, Dictionary<string, string> useKindAndNames, string inputType)
		{
			bool flag = true;
			List<string> list = new List<string>
			{
				Resources.Str_FamilyKindName_FireWall,
				Resources.Str_FamilyDbKindName_FireDoor,
				Resources.Str_FamilyDbKindName_FireWindow,
				Resources.Str_FamilyDbKindName_FireRoolDoor,
				Resources.Str_FamilyKindName_FrontRoomDoor
			};
			for (int i = 0; i < inputValues.Count; i++)
			{
				if (inputValues[i].UseRevitLinkInstance == null && inputValues[i].ThisElement is FamilyInstance)
				{
					string text = list[(int)inputValues[i].ThisType];
					FamilyInstance useFamilyInstance = inputValues[i].ThisElement as FamilyInstance;
					if (useKindAndNames.ContainsKey(text))
					{
						flag = (flag && BaseReplacer.ReplaceForFamilyInstance(useFamilyInstance, useKindAndNames[text], text, inputType));
						if (flag)
						{
							inputValues.Remove(inputValues[i]);
							i--;
						}
					}
				}
			}
			return flag;
		}

		private static bool ReplaceForFamilyInstance(FamilyInstance useFamilyInstance, string useFamilyName, string useKindName, string inputType)
		{
			Family afamily = YJKRoomInspection.Utiltiy.Utility.GetAFamily(useFamilyInstance.Document, useFamilyName, false, useKindName, true);
			return YJKRoomInspection.Utiltiy.Utility.FamilyReplace(useFamilyInstance, afamily, inputType, true);
		}
	}
}
